/**
 * Copyright (c) 2007-2008 Nearchos Paspallis
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

/**
 * AspectSense
 *
 * Middleware for annotation-based, context-aware and adaptive services for
 * mobile and ubiquitous computing
 *
 * This project is an open-source effort, initiated by Nearchos Paspallis as
 * part of his PhD work at the University of Cyprus (http://www.ucy.ac.cy).
 *
 * More information about the project is available at "http://aspectsense.org".
 */

package org.aspectsense.graphs;

import java.util.List;
import java.util.LinkedList;
import java.util.Set;
import java.util.HashSet;

/**
 * @author Nearchos Paspallis [nearchos@cs.ucy.ac.cy]
 *         Date: Dec 29, 2007
 *         Time: 6:09:59 PM
 */
public class DefaultDirectedBipartitePath<E> implements DirectedPath<E>, Bipartite<E>
{
    private final List<DirectedEdge<E>> orderedDirectedEdges
            = new LinkedList<DirectedEdge<E>>();

    private final Set<Node<E>> leftPartition = new HashSet<Node<E>>();
    private final Set<Node<E>> rightPartition = new HashSet<Node<E>>();

    public DefaultDirectedBipartitePath(final List<DirectedEdge<E>> orderedDirectedEdges, final boolean startFromLeftPartition)
    {
        super();

        // evaluateDouble
        Node<E> previousToNode = null;
        for(final DirectedEdge<E> directedEdge : orderedDirectedEdges)
        {
            final Node<E> fromNode = directedEdge.getFromNode();
            final Node<E> toNode = directedEdge.getToNode();
            if(fromNode == null || toNode == null)
            {
                throw new IllegalArgumentException("Nodes cannot be null; "
                        + "directed edge: " + directedEdge);
            }

            if(previousToNode != null)
            {
                if(!previousToNode.equals(fromNode))
                {
                    throw new IllegalArgumentException("The given directed "
                            + "edges are not continuous!");
                }
            }

            previousToNode = toNode;
        }

        for(final DirectedEdge<E> directedEdge : orderedDirectedEdges)
        {
            this.orderedDirectedEdges.add(directedEdge);
            if(leftPartition.isEmpty())
            {
                if(startFromLeftPartition)
                {
                    leftPartition.add(directedEdge.getFromNode());
                    rightPartition.add(directedEdge.getToNode());
                }
                else
                {
                    leftPartition.add(directedEdge.getToNode());
                    rightPartition.add(directedEdge.getFromNode());
                }
            }
            else
            {
                if(leftPartition.contains(directedEdge.getFromNode()))
                {
                    rightPartition.add(directedEdge.getToNode());
                }
                else if(rightPartition.contains(directedEdge.getFromNode()))
                {
                    leftPartition.add(directedEdge.getToNode());
                }
                else
                {
                    // illegal state
                    throw new IllegalArgumentException("The path does not "
                            + "appear to be contigues; edge not connected to "
                            + "the rest of path: " + directedEdge);
                }
            }
        }
    }

    public Node<E> getPathStart()
    {
        return orderedDirectedEdges.get(0).getFromNode();
    }

    public Node<E> getPathEnd()
    {
        return orderedDirectedEdges.get(getNumberOfEdges() - 1).getToNode();
    }

    public List<DirectedEdge<E>> getOrderedEdges()
    {
        return new LinkedList<DirectedEdge<E>>(orderedDirectedEdges);
    }

    public int getNumberOfEdges()
    {
        return orderedDirectedEdges.size();
    }

    public Set<Node<E>> getLeftPartition()
    {
        return new HashSet<Node<E>>(leftPartition);
    }

    public Set<Node<E>> getRightPartition()
    {
        return new HashSet<Node<E>>(rightPartition);
    }
}